package fun.hijklmn.magic.image.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import fun.hijklmn.magic.common.exception.MagicBizException;
import fun.hijklmn.magic.common.utils.UUIDUtil;
import fun.hijklmn.magic.image.common.pojo.Image;
import fun.hijklmn.magic.image.server.service.ImageAdminService;
import fun.hijklmn.magic.image.server.service.ImageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @Description
 * @Author fs
 * @Date 2021/5/21 11:49
 * @Version 0.0.1-SNAPSHOT
 **/
@Service
public class ImageAdminServiceImpl implements ImageAdminService {

    @Autowired
    private ImageService imageService;

    @Override
    public Image save(Image image) {

        LocalDateTime curLocalDateTime = LocalDateTime.now();

        if (StringUtils.isNotEmpty(image.getId())) {
            image.setId(UUIDUtil.uuid());
            image.setCreateTime(curLocalDateTime);
            image.setIsDelete(false);
            imageService.save(image);
            return image;
        } else {
            Image image1 = imageService.getById(image.getId());
            if (image1 != null && !image1.getIsDelete()) {
                image.setUpdateTime(curLocalDateTime);
                imageService.update(image, new UpdateWrapper<Image>().eq("id", image.getId()));
                return image;
            }

            throw new MagicBizException("记录被删除");

        }

    }

}
